<template xmlns="http://www.w3.org/1999/html">
  <div id="app">
    <home-btn></home-btn>
    <return-btn v-show="['首页'].indexOf(this.pageName) === -1" ></return-btn>
    <div v-show="countdown<=10&countdown>0" style="font-weight:bolder ;border-radius: 6px; padding:10px;font-size: larger;color: white; background-color: rgba(255,107,107,0.5); z-index: 1000; width:100px;position: absolute;top:20px;left:calc(100vw - 400px);text-align: center;">
      自动退出登录倒计时{{countdown}}秒,点击任意地方可暂不退出

    </div>
    <div class="header">


      <div class="logo-box">
        <span class="app-name" @dblclick="$router.push({path:'/devtest'})">福你就业智能服务窗口</span>
      </div>
      <div class="user-box" v-show="login">

        <div class="menu" @click="clickUserNameHandler">
          <van-icon size="0.7rem" color="white" name="user-circle-o" style="margin-top: 10px"/>
          <div class="text">{{nameHide(userInfo?(userInfo.xm0000?userInfo.xm0000:(userInfo.nc0000?userInfo.nc0000:userInfo.sjhm00)):"")}}</div>
        </div>
        <div class="menu" @click="logout">
          <van-icon size="0.7rem" color="white" name="share-o" style="margin-top: 10px;"/>
          <div class="text">退出</div>
        </div>

      </div>
    </div>
    <div style="text-align: left">
      <div style="position: fixed; top: 20px; left: 15px; height: 40px; margin-left: 30px;color: #d9e0ee; font-size: 24px;display: none">
        <i class="el-icon-position"></i>
        {{pageName}}
      </div>
    </div>
    <div style="height: calc(100vh - 80px);width:100%; overflow-y: auto;background-color: #3087ea;">
      <keep-alive>
        <router-view v-if="$route.meta.keepAlive"></router-view>
      </keep-alive>
      <router-view v-if="!$route.meta.keepAlive"></router-view>
    </div>
    <span ref="ltMsg"  style="position: fixed;top:290px;right: 156px;color: #409eff;font-weight: bold;z-index: 1000;font-size: 14px;display: none" >嗨！点我帮您找工作</span>
    <img ref="lt1" style="position:fixed;width: 15px; height: 15px; top: 360px; right: 140px;z-index: 100;display: none" src="@/assets/lt1.png">
    <img ref="lt2" style="position:fixed;width: 20px; height: 20px; top: 340px; right: 150px;z-index: 100;display: none" src="@/assets/lt2.png">
    <img ref="lt3" style="position:fixed;width: 150px; top: 240px; right: 140px;z-index: 100;display: none" src="@/assets/lt3.png">
    <img ref="xnrZb" style="position:fixed;width: 12px; top: 378px; right: 77px;z-index: 100;display: block" src="@/assets/xnr-zb.png">

    <img  class="woman-icon" style="position: fixed;height: 260px;top:270px;right: 10px" src="@/assets/xnr002.png" @click="startAi" />
    <img style="position: fixed;width: 150px;height: 150px;top:690px;right: 10px" src="@/assets/wechat_qrcode.jpg"/>

    <el-dialog
        class="ai-nav-dia"
        title="就业助理"
        :visible.sync="aiNavVisible"
        width="90%"
        center>
      <div style="display: flex;">
        <div style="width: 200px;">
          <img :src="xnr" style="height: 300px;"/>
        </div>
        <div style="flex: 1;overflow-y: scroll;height: 600px;color: #000000" ref="aiScrollDiv">
          <transition-group name="shake" tag="div">

            <div style="position: relative;border: 1px solid #dedede;padding: 30px;padding-bottom: 60px; margin-top: 20px;border-radius: 40px;background-color: #ecf3ff" v-for="(item,index) in chatList" :key="index">
              <span class="chat-box-arrow"></span>
              <div  style="font-weight: bolder;font-size: large;">
                {{item.question.text}}
              </div>
              <div style="margin-top: 50px" >
                <transition-group name="fly-in" tag="span">
                  <span @click="aiNavHandle(it)" v-for="(it,i) in item.select" class="sel-item" :key="i" style="background-color: white; border: 1px solid #dedede;padding: 30px;border-radius: 20px;font-weight: bolder;cursor: pointer;font-size: x-large;margin-right: 15px;  box-shadow: 4px 4px 4px 0 rgba(0, 0, 0, 0.5);color: #409eff">  {{it.text}}</span>
                </transition-group>
              </div>

            </div>
          </transition-group>
        </div>
      </div>
    </el-dialog>
    <audio ref="audioPlayer" @ended="endedSpeak" @error="endedSpeak"  @pause="endedSpeak" :src="audi"> </audio>
  </div>
</template>

<script>

import {
  getUser,
  removeToken,
  removeUser,
} from "@/utils/auth";
import {loginApi} from "@/api";

export default {
  name: 'App',
  components: {
  },
  data() {
    return {
      xnrOpenMouse:new URL("@/assets/xnr002-zz.png", import.meta.url).href,
      xnrCloseMouse:new URL("@/assets/xnr002.png", import.meta.url).href,
      xnr:new URL("@/assets/xnr002.png", import.meta.url).href,
      logoXnr:new URL("@/assets/xnr002.png", import.meta.url).href,
      audi:new URL("@/assets/ai-adi/nihao.wav", import.meta.url).href,
      emptyAudi:new URL("@/assets/ai-adi/blank.wav", import.meta.url).href,
      chatFrameIndex:0,
      chatFrame:[new URL("@/assets/liaot6.png", import.meta.url).href,new URL("@/assets/liaot4.png", import.meta.url).href,new URL("@/assets/liaot5.png", import.meta.url).href,new URL("@/assets/liaot3.png", import.meta.url).href],
      aiOutTm:"",
      aiSpeakInterval:"",
      aiNavVisible:false,
      pageName:"首页",
      userInfo:null,
      curDate: "",
      curTime: "",
      login: false,
      countdown: 0,
      countdownInterval: null,
      chatList:[

      ],
      myTimeDisplay:"",
      aiNav:{
        steps:{
          "001":{
            no:"001",
            question:{
              text:"您好，我是您的就业小助理，小福，请问您想办理什么业务?",
              adi: new URL("@/assets/ai-adi/nihao.wav", import.meta.url).href
            },
            select:[
              {
                text:"找工作",
                action:"ai",//route  ai
                target:"002"
              },
              {
                text:"了解政策",
                action:"route",//route  ai
                target:"/policy"
              },
              {
                text:"参加技能培训",
                action:"route",//route  ai
                target:"/skills-training"
              }
            ]

          },
          "002":{
            no:"002",
            question:{
              text:"请问您是要找全职工作还是打零工?",
              adi:new URL("@/assets/ai-adi/zgz-qz-lg.wav", import.meta.url).href
            },
            select:[
              {
                text:"找全职",
                action:"route",//route  ai
                target:"/position-query-qz"
              },
              {
                text:"打零工",
                action:"route",//route  ai
                target:"/position-query-lg"
              },
            ]
          }
        }

      }
    }
  },
  watch:{
    '$route'(to,from){
      this.pageName=to.meta.title
    },
    aiNavVisible(val){

      if(val){
        this.endedSpeak()

      }else{
        this.chatList=[]
        this.endedSpeak()
        this.pauseSpeak()
      }
    }
  },
  mounted() {
    window.addEventListener("click", this.resetCountdown);


    this.userInfo = getUser()
    if (this.userInfo){
      this.login = true
      this.startCountdown()
    } else {
      this.login = false
    }
   this.initBubblingLt()
   this.initQQMap()
  },
  methods: {
    clickUserNameHandler(){
      console.log(this.userInfo)
      switch (this.userInfo.grsmzt){
        case "1":{
          this.$router.push({path:'/person-center'})
          break;
        }
        case "0":{
          this.toRealName()
          break;
        }
        case "2":{
          break;
        }
        case "3":{
          break;
        }
        case "4":{
          this.toRealName()
          break;
        }
      }


    },
    toRealName(){
      this.$router.push({path:'/person-real-name'})

    },
    initBubblingLt(){
      let lt1=this.$refs.lt1;
      let lt2=this.$refs.lt2;
      let lt3=this.$refs.lt3;
      let ltMsg=this.$refs.ltMsg;
      let xnrZb=this.$refs.xnrZb;
      let index=0;
      let openMouse=false
      setInterval(()=>{
        if(index>=8){
          lt1 .style.display="none"
          lt2.style.display="none"
          lt3.style.display="none"
          ltMsg.style.display="none"
          index=0
        }
        if(index==2){
          lt1.style.display="block"
        }
        if(index==3){
          lt2.style.display="block"
        }
        if(index>4){
          lt3.style.display="block"
          ltMsg.style.display="block"

        }
        index++

        if(openMouse){
          xnrZb.style.display="none"
          openMouse=false
        }else{
          xnrZb.style.display="block"
          openMouse=true
        }
      },400)

    },
    initQQMap(){
      const s = document.createElement('script');
      s.type = 'text/javascript';
      s.src = "https://map.qq.com/api/gljs?v=1.exp&key=Z6JBZ-NTSWN-77FFY-SFWDT-GRBS2-3SBNN";
      document.body.appendChild(s);
    },
    endedSpeak(){
      if(this.aiSpeakInterval){
        clearInterval(this.aiSpeakInterval)
      }
      this.aiSpeakInterval=""
      this.xnr=this.xnrCloseMouse

    },
    resetCountdown() {
      this.countdown = 120
    },
    startCountdown() {
      this.countdown = 120
      clearInterval(this.countdownInterval );//销毁之前定时器
      this.countdownInterval = setInterval(() => {
        if(this.countdown==11){
           console.log("进入10秒倒计时")
        }
        if (this.countdown <= 0){
          this.logout()
        }
        this.countdown--; //每秒更新一次时间
      }, 1000)
    },
    nameHide(name) {
      if (name.length == 2) {
        name = name.substring(0, 1) + '*'; // 截取name 字符串截取第一个字符，
        return name; // 张三显示为张*
      } else if (name.length == 3) {
        name = name.substring(0, 1) + '*' + name.substring(2, 3); // 截取第一个和第三个字符
        return name; // 李思思显示为李*思
      } else if (name.length > 3) {
        name = name.substring(0, 1) + '*' + '*' + name.substring(3, name.length); // 截取第一个和大于第4个字符
        return name; // 王二麻子显示为王**子
      }
    },

    handleScrollBottom() {
      this.$nextTick(() => {
        let scrollElem = this.$refs.aiScrollDiv;
        scrollElem.scrollTo({ top: scrollElem .scrollHeight, behavior: 'smooth' });
      });
    },
    aiMessageOut(fullText,message){

      if (this.aiOutTm) {
        clearInterval(this.aiOutTm);
      }
      this.aiOutTm = setInterval(() => {
        if (fullText.length > message.question.text.length) {
          message.question.text += fullText[message.question.text.length];
        } else {
          clearInterval(this.aiOutTm);
          this.aiNav.steps[message.no].select.forEach((item,i)=>{
            setTimeout(()=>{
              message.select.push({... item})
            },i*500)

          })
          this.handleScrollBottom()
        }
      }, 200);
    },
    aiNavHandle(item){
      if(item.action=='ai'){
        this.audi=this.aiNav.steps[item.target].question.adi
        this.playSpeak(item.target)

      }else{
        this.aiNavVisible=false
        this.$router.push({path:item.target})
      }

    },
    startAi(){
      this.chatList=[]
      this.aiNavVisible=true
      setTimeout(()=>{
        this.audi=this.aiNav.steps['001'].question.adi
        this.playSpeak("001")
      },2000)
    },
    playSpeak(step){
      setTimeout(()=>{
        const audioPlayer = this.$refs.audioPlayer;
        try{
          audioPlayer.load()
          audioPlayer.play()
          setTimeout(()=>{
            let openMouse=false
            if(this.aiSpeakInterval){
              clearInterval(this.aiSpeakInterval)

            }
            this.aiSpeakInterval=setInterval(()=>{
              if(openMouse){
                this.xnr=this.xnrCloseMouse
              }else{
                this.xnr=this.xnrOpenMouse
              }
              openMouse=!openMouse
            },200)
          },800)

        }catch (e){

        }
        //您好，我是您的就业小助理，小福，请问您想办理什么业务？
        const  message=JSON.parse(JSON.stringify(this.aiNav.steps[step]))
        message.select=[]
        this.chatList.push(message)
        const fullText=message.question.text
        message.question.text=""
        setTimeout(()=>{
          this.aiMessageOut(fullText,message)
        },1500)

      },10)

    },
    pauseSpeak(){
      this.endedSpeak()
      setTimeout(()=>{
        const audioPlayer = this.$refs.audioPlayer;
        try{
          audioPlayer.pause()
        }catch (e) {
        }finally {
          try{

            audioPlayer.src=this.emptyAudi
            audioPlayer.load()
          }catch (ex){

          }
        }

      },50)
    },
    toLogin() {
      if ('/login' !== this.$route.path){
        this.$router.push('/login')
      }
    },
    getLoginInfo(){
      this.userInfo = getUser()
      if (this.userInfo){
        this.login = true
        this.startCountdown()
      } else {
        this.login = false
      }
    },
    logout(){
      loginApi.logout()
          .then(()=>{
          }).finally(()=>{
        removeUser()
        removeToken()
        this.userInfo = null
        this.login = false
        clearInterval(this.countdownInterval );//销毁之前定时器
        window.location.href="/"

      })
    }
  }
}
</script>

<style lang="scss">
#app {
  position: relative;
  border-radius: 12px;
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  height: 100%;
  width: 100%;
  text-align: center;
  color: #2c3e50;
  display: flex;
  flex-direction: column;
  font-family: microsoft yahei, Arial, Helvetica, sans-serif;
  .header{
    position: relative;
    width: 100%;
    height: 80px;
    text-align: center;
    .logo-box{
      width: 600px;
      margin: auto;
      display: flex;
      align-items: center;
      height: 100%;
      .icon{
        height: 70px;
        width: 70px;
        margin-left: 30px;
      }
      .app-name{
        text-align: left;
        flex: 1;
        margin-left: 50px;
        font-size: 46px;
        font-weight: bold;
        color: white;
        display: inline-block;
      }

    }
    .user-box{
      position: absolute;
      top:10px;
      right: 100px;
      display: flex;
      align-items: center;
      .user{
        color: white;
        font-size: x-large;
        .logout{
          font-size: large;
          cursor: pointer;
          margin-left: 5px;
        }
      }
      .menu{
        cursor: pointer;
        width: 76px;
        height: 76px;
        margin-left: 4px;
        border-radius: 20px;
        background: linear-gradient(#25A4DE, #02BA90);
        .text{
          font-size: 16px
        ;
          color: white;
          overflow-x: hidden;
        }
      }
    }
    background-color: #3087ea;
  }
  .yydh-btn{
    width: 100px;
    height: 100px;
    border: 1px solid rgba(187,187,187,1);
    position: fixed;
    right: 20px;
    top: 30px;
    border-radius: 50px;
    text-align: center;
    line-height: 62px;
    font-weight: bolder;
    font-size: 30px;
    color: white;
    cursor: pointer;

  }
  .system-logo-icon {
    width: 250px;
    z-index: 1;
    left: 2px;
    top: 380px;
    position: fixed;
  }
}
.ai-nav-dia{
  .el-dialog {
    position: relative;
    margin: 0 auto 50px;
    margin-top: 0px;
    background-color: white;
    border-radius: 20px;
    -webkit-box-shadow: 0 1px 3px rgba(0,0,0,.3);
    box-shadow: 0 1px 3px rgba(0,0,0,.3);
    box-sizing: border-box;
    width: 50%;
    border: 1px solid #ffffff;
  }
  .el-dialog__title {
    line-height: 24px;
    font-size: 26px;
    color: #000000;
  }
}
.chat-box-arrow{
  border: 8px solid #fff;
  border-right: 8px solid #fff;
  position: absolute;
  top: 6px;
  left: -30px;
}
/* 定义抖动动画 */
.shake-enter-active, .shake-leave-active {
  animation: shake 1s;
}

/* 设置初始状态和结束状态的偏移量 */
.shake-enter, .shake-leave-to /* 2.1.8版本及以上使用v-leave-to代替v-leave-active */ {
  transform: translateX(0);
}

/* 定义动画 */
@keyframes shake {
  0% { transform: translateX(0); }
  10% { transform: translateX(-5px); }
  20% { transform: translateX(5px); }
  30% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  50% { transform: translateX(-5px); }
  60% { transform: translateX(0); }
  100% { transform: translateX(0); }
}
.fly-in-enter-active, .fly-in-leave-to {
  transition: opacity 1s, transform 2s;
}
.fly-in-enter, .fly-in-leave-to /* 2.1.8版本及以上使用v-leave-to代替v-leave-active */ {
  opacity: 0;
  transform: translateY(20px);
}
.sel-item{
  margin-top: 10px;
}
.woman-icon {
  &:hover {
    cursor: pointer;
  }
}
</style>
